package com.eclipse.e0112_多线程.聊天;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client {
	private static Scanner scanner = new Scanner(System.in);
	private static String ip = "127.0.0.1";
	private static int port = 8080;

	public static void main(String[] args) throws IOException {
		Socket socket = new Socket(ip, port);
		OutputStream outputStream = socket.getOutputStream();
		InputStream inputStream = socket.getInputStream();

		BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(outputStream));

		ClientMesh cm = new ClientMesh(in);
		cm.start();
		while (true) {
			System.out.println("客户端输入信息:");
			String info = scanner.nextLine();
			out.write(info + "\r\n");
			out.flush();
		}

	}
}

class ClientMesh extends Thread {
	private BufferedReader in;

	ClientMesh(BufferedReader in) {
		this.in = in;
	}

	public void run() {
		String info = null;
		while (true) {

			try {
				info = in.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.out.println("客户端收到信息" + info);

		}
	}
}